package leetcode;


import java.util.*;

/**
 * @Classname FindLucky_5368
 * @Author happytaohaha
 * @Date 2020/3/29
 * @Description 找出幸运值，一个Map搞定  O（N）
 */
public class FindLucky_5368 {
    public static int findLucky(int[] arr) {
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < arr.length; i++) {
            if (!map.containsKey(arr[i])) {
                map.put(arr[i], 1);
            } else {
                int val = map.get(arr[i]);
                map.put(arr[i], val + 1);
            }
        }
        int result = -1;
        Iterator iterator = map.keySet().iterator();
        while (iterator.hasNext()) {
            int key = (int) iterator.next();
            if (key == map.get(key)) {
                result = result > key ? result : key;
            }
        }
        return result;
    }

    public static void main(String[] args) {
        System.out.println(findLucky(new int[]{5}));
    }
}
